package xmtTest;

import epam_java.jsontest.UserTest;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class XMLWriter {

    private DocumentBuilder builder;
    private String filename;
    private ArrayList<UserTest> users;

    public XMLWriter(String filename, ArrayList<UserTest> users) {
        this.filename = filename;
        this.users = users;
    }

             void writeXML() throws TransformerConfigurationException, TransformerException, FileNotFoundException, ParserConfigurationException {
        if(users!=null){
      builder=getDocumentBuilder();
      Document doc = buildDocument();
      Transformer t = TransformerFactory.newInstance().newTransformer();
      t.setOutputProperty(OutputKeys.VERSION,"1.0");
      t.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
      t.transform(new DOMSource(doc), new StreamResult(new FileOutputStream(filename)));
        }
    }

    private Document buildDocument() throws DOMException {
        Document doc = builder.newDocument();
        Element usersElement = doc.createElement("users");
        doc.appendChild(usersElement);
        usersElement.setAttribute("xsi:noNamespaceSchemaLocation", "user_xsd.xsd");
        usersElement.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
        for (UserTest user : users) {
            Element userElement = doc.createElement("user");
            usersElement.appendChild(userElement);
            Element userIdElement = doc.createElement("userId");
            userIdElement.setTextContent("" + user.getUserId());
            userElement.appendChild(userIdElement);
            Element userNameElement = doc.createElement("userName");
            userNameElement.setTextContent("" + user.getUserName());
            userElement.appendChild(userNameElement);
            Element userLoginElement = doc.createElement("login");
            userLoginElement.setTextContent("" + user.getLogin());
            userElement.appendChild(userLoginElement);
            Element userAccessElement = doc.createElement("currentAcces");
            userAccessElement.setTextContent("" + user.getCurrentAcces());
            userElement.appendChild(userAccessElement);
            Element intNumbersElement = doc.createElement("intfavouriteNumbers");
            for (int value : user.getIntfavouriteNumbers()) {
                Element intValue = doc.createElement("intValue");
                intValue.setTextContent("" + value);
                intNumbersElement.appendChild(intValue);
            }
            userElement.appendChild(intNumbersElement);
            Element doubleNumbersElement = doc.createElement("doublefavouriteNumbers");
            for (double value : user.getDoublefavouriteNumbers()) {
                Element doubleValue = doc.createElement("doubleValue");
                doubleValue.setTextContent("" + value);
                doubleNumbersElement.appendChild(doubleValue);
            }
            userElement.appendChild(doubleNumbersElement);
        }
        return doc;
    }
    DocumentBuilder getDocumentBuilder() throws ParserConfigurationException{
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        return builder;
    }
}
